//
// Created by admin on 2024/8/2.
//
#include <Wire.h>
#include <Arduino.h>
uint8_t adress = 0x38; //设备地址 默认7位2进制数
float T;

float RH;

int data[6];
void AHT10_measure(void);
void setup()
{

    Wire.begin();
    Serial.begin(9600);

}

void loop()
{

    AHT10_measure();
    delay(2000);

}

void AHT10_measure()
{

    Wire.beginTransmission(0x38);

    Wire.requestFrom(0x38, 6);

    while (Wire.available())
    {
        for (int i = 0; i < 6; i++)
        {
            data[i] = Wire.read();
            // Serial.println(data[i]);
        }

        if (data[0] & 0x08 == 0)
        {
            // Serial.println("进行初始化");
            // Serial.println(Wire.read());
            Wire.write(0xE1);
            Wire.write(0x08);
            Wire.write(0x00);
        }
        else
        {
            Serial.println("不需要初始化");
            // Serial.println(Wire.read());
            Wire.write(0xAC);
            Wire.write(0x33);
            Wire.write(0x00);
            delayMicroseconds(75);


            if (data[0] & 0x80 == 0)
            {
                Serial.println("需要等待完成");
                // Serial.println(Wire.read());
            }
            else
            {
                Serial.println("不需要等待");
                // Serial.println(Wire.read());
            }
        }
    }
    Wire.endTransmission();
    RH = ((data[1] << 12) | (data[2] << 4)) | (data[3] >> 4);
    T = ((data[3] & 0x0f) << 16) | (data[4] << 8) | data[5];
    RH = (RH / pow(2, 20)) * 100;
    T = (T / pow(2, 20)) * 200 - 50;
    Serial.print("湿度为");
    Serial.println(RH);
    Serial.print("温度为");
    Serial.println(T);

}

